package controller

import (
	"digital/server/common"
	"digital/server/model"
	"digital/server/response"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func GetNoticemanage(ctx *gin.Context) {
	Uid := ctx.PostForm("uid")
	var notices []model.Notice
	// 构建数据库查询条件
	DB := common.GetDB()
	err := DB.Where("uid = ? ", Uid).Find(&notices).Error
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "公告不存在",
		})
		return
	}
	response.Success(ctx, gin.H{"notices": notices}, "查询成功")
}
func DeleteNotice(ctx *gin.Context) {
	Nid := ctx.PostForm("nid")
	print(Nid)
	var notice model.Notice
	// 构建数据库查询条件
	DB := common.GetDB()
	err := DB.Where("nid = ? ", Nid).Delete(&notice).Error
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"success": false,
			"msg":     "公告删除失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"success": true,
		"msg":     "公告删除成功",
	})
}
func AddNotice(ctx *gin.Context) {
	var notice model.Notice
	if err := ctx.ShouldBind(&notice); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}

	notice.Ntime = time.Now() // 设置当前时间

	DB := common.GetDB()
	if err := DB.Create(&notice).Error; err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "数据库插入失败",
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"success": true,
		"msg":     "公告发布成功",
	})
}
